


window.onload = function () {

	load();

	function load() {
	new CAAT.Module.Preloader.Preloader().

		addElement("dude", "img/dude.png").
        addElement("girl", "img/hotgirl2.png").
        addElement("background-1", "img/background-1.png").
        addElement("jumbButton", "img/buttons/kim.png").
        addElement("actionButton", "img/buttons/hoa.png").
        addElement("menu_bg", "img/menu_bg.png").
        addElement("menu_button", "img/buttons/menu_button2.png").
        addElement("cancelButton", "img/cancelButton.png").
        addElement("credits_bg", "img/credits_bg.png").
        addElement("menuBattleButton", "img/buttons/menuButton.png").
        addElement("menuListButton", "img/buttons/menu_battle_button.png").
        addElement("censored", "img/demoCensore.png").
        addElement("brick", "img/brickBlock.png").
        addElement("rock", "img/rock_icon.png").
		load(function onAllAssetsLoaded(images) {
                run(images);
            }
        );
	}

	function run(images) {
		CAAT.DEBUG = 1;
		var director = new CAAT.Director().initialize(CANVAS_WIDTH, CANVAS_HEIGHT, document.getElementById("canvas"));
        director.setImagesCache(images);

        var sceneMenu = director.createScene();
        
        var sceneBattleIndex = 1;
        
        
        var sceneGame = director.createScene();
        var sceneMenuBattle = director.createScene();


                            var sceneGameContainer = new CAAT.SceneGameCtn().create(director);
                            sceneGame.addChild(sceneGameContainer);

                            var menu = new CAAT.MenuActor().initialize(director, sceneBattleIndex,sceneGameContainer);
                            sceneMenu.addChild(menu);   

                            var sceneMenuIndex = 0;
                            var sceneBattleIndex = 1;
                            var sceneMenuBattleIndex = 2;
                            var lastTime = 0;
                            var t = 0;
                            var sceneTime = 0;
                            var menuBattleContainer;
                                sceneGame.createTimer(0,Number.MAX_VALUE,
                                function (scene_time, timer_time, timertask_instance) {   // timeout

                                },
                                function (scene_time, timer_time, timertask_instance) {   // tick
                                        var bc = sceneGame.getChildAt(0);
                                        if(bc.paused){
                                            bc.paused = false;
                                            director.switchToScene(sceneMenuBattleIndex);
                                            menuBattleContainer = new CAAT.MenuBattleContainer().initialize(bc,null,sceneBattleIndex);
                                            sceneMenuBattle.emptyChildren();
                                            sceneMenuBattle.addChild(menuBattleContainer);
                                        }
                                        var dt = scene_time - lastTime;//Khoang thoi gian giua 2 lan cap nhat
                                        lastTime = scene_time;
                                        t += dt*GAME_SPEED;//Thoi gian delay giua 2 lan cap nhat
                                        while (t >= frameTime) {//Chay chi khi thoi gian delay giua 2 lan lon hon 10ms
                                            t -= frameTime;//Dung de xac dinh so buoc' tinh toan
                                            sceneTime += frameTime;
                                            sceneGameContainer.update(director, sceneTime);
                                        }
                                        
                                },
                                function (scene_time, timer_time, timertask_instance) {   // cancel

                                }
                            );
        CAAT.loop(60);        
	}

}